package com.market.product.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.market.category.dao.CategoryDao;
import com.market.category.domain.CategoryCommand;
import com.market.product.dao.ProductDao;
import com.market.product.domain.ProductCommand;
import com.market.product.validator.ProductValidator;
import com.market.util.FileUtil;

@Controller
public class ProUpdateController {
	@Autowired
	private ProductDao productDao;
	@Autowired
	private CategoryDao categoryDao;

	@RequestMapping("/productShowToggle.do")
	public String toggle(@RequestParam("p_code") int p_code,
			@RequestParam("pageNum") int pageNum,
			@RequestParam("searchMarket") String searchMarket,
			@RequestParam("searchCategory") String searchCategory){
		ProductCommand productCommand = productDao.select(p_code);		

		if(productCommand.getShow().equals("y"))
			productCommand.setShow("n");
		else
			productCommand.setShow("y");

		productDao.update(productCommand);
		return "redirect:/productList.do?pageNum="+pageNum+"&searchMarket="+searchMarket+"&searchCategory="+searchCategory;
	}

	@RequestMapping(value="/productUpdate.do",method=RequestMethod.GET)
	public ModelAndView form(@RequestParam("p_code") int p_code,
			@RequestParam("c_code") int c_code,
			@RequestParam("pageNum") int pageNum,
			@RequestParam("searchMarket") String searchMarket,
			@RequestParam("searchCategory") String searchCategory){		

		ProductCommand productCommand = productDao.select(p_code);
		List<CategoryCommand> market = categoryDao.selectedMarket(c_code);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("keyWord", categoryDao.select(c_code).getMarket());
		List<CategoryCommand> category = categoryDao.list(map);

		ModelAndView mav = new ModelAndView();
		mav.setViewName("productUpdate");
		mav.addObject("command", productCommand);
		mav.addObject("market", market);
		mav.addObject("category", category);
		mav.addObject("pageNum",pageNum);
		mav.addObject("searchMarket",searchMarket);
		mav.addObject("searchCategory",searchCategory);
		return mav;
	}

	@RequestMapping(value="/productUpdate.do",method=RequestMethod.POST)
	public String submit(@ModelAttribute("command")ProductCommand productCommand,
			BindingResult result,
			@RequestParam("pageNum") int pageNum,
			@RequestParam("searchMarket") String searchMarket,
			@RequestParam("searchCategory") String searchCategory){

		new ProductValidator().validate(productCommand, result);		

		if(result.hasErrors()){
			return "productUpdate";
		}

		ProductCommand product = productDao.select(productCommand.getP_code());
		List<MultipartFile> list = productCommand.getUpload();		
		String oldFileNames = product.getImage();		
		String newNames = "";
		
		try{
			if(!list.get(0).isEmpty()){
				for(int i=0;i<list.size();i++){
					if(i>0)
						newNames += "&";
					newNames += i + FileUtil.rename(list.get(i).getOriginalFilename());					
				}
				productCommand.setImage(newNames);
			}else{
				productCommand.setImage(oldFileNames);
			}			
			
			productDao.update(productCommand);
			
			String newName[] = newNames.split("&");
			
			if(!list.isEmpty()){
				for(int i=0;i<list.size();i++){
					File file = new File(FileUtil.UPLOAD_PATH+"/" + newName[i]);
					list.get(i).transferTo(file);
				}
				
				if(oldFileNames != null){
					for(String s : oldFileNames.split("&")){
						FileUtil.removeFile(s);						
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return "redirect:/productList.do?pageNum="+pageNum+"&searchMarket="+searchMarket+"&searchCategory="+searchCategory;
	}
}
